package com.java.travel.service.impl;

import com.java.travel.dao.UserDao;
import com.java.travel.entity.User;
import com.java.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * description:
 * author:张腾
 * date:2021-03-07
 */

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void register(User user) {
        if(userDao.findUserByUserName(user.getUsername())==null){
            userDao.save(user);
        }else{
            throw new RuntimeException("用户名已存在！");
        }

    }

    @Override
    public User login(User user) {
        User userDB = userDao.findUserByUserName(user.getUsername());
        if (userDB!=null){
            if (userDB.getPassword().equals(user.getPassword())){
                return userDB;
            }
            throw new RuntimeException("密码输入错误！");
        }else{
            throw new RuntimeException("用户名输入错误！");
        }
    }
}
